package xujiwen.transport.Mgr;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import xujiwen.transport.bean.PassRecord;
import xujiwen.transport.util.DBConnect;

public class PassRecordMgr {
	public void Save(final PassRecord passRecord) {
		final DBConnect db = new DBConnect();
		final String sql = "insert into PASSRECORD values (null, ?, ?, ?, ?, ?, ?, now())";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setString(1, passRecord.getIcid());
			db.setString(2, passRecord.getCid());
			db.setDouble(3, passRecord.getDealSum());
			db.setString(4, passRecord.getEntryName());
			db.setString(5, passRecord.getExitName());
			db.setTimestamp(6, new Timestamp(passRecord.getEntryDate().getTime()));
			db.excute();
			db.commit();

		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
	
	// 根据进出口站 或者车牌号查询
	public List<PassRecord> GetSearchPassList(String str) {
		final List<PassRecord> pass = new ArrayList<PassRecord>();
		final DBConnect db = new DBConnect();
		final String sql;
		if(str.length() > 0)
		sql = "select * from PASSRECORD where EntryName like '%" + str +"%' or ExitName like '%" + str +"%'" + " or CID like '%" + str +"%'";
		else sql = "select * from PASSRECORD limit 10";
		//System.out.println(sql);
		db.prepareStatement(sql);
		db.excuteQuery();
		try {
			while (db.next()) {
				final PassRecord passRecord = new PassRecord();
				passRecord.setPid(db.getInt("PID"));
				passRecord.setIcid(db.getString("ICID"));
				passRecord.setCid(db.getString("CID"));
				
				passRecord.setDealSum(db.getDouble("DEALSUM"));
				passRecord.setEntryName(db.getString("EntryName"));
				passRecord.setExitName(db.getString("ExitName"));
				
				passRecord.setEntryDate(db.getTimestamp("ENTERDATE"));
				passRecord.setExitDate(db.getTimestamp("EXITDATE"));
				pass.add(passRecord);
			}
		} catch (final Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {
			db.close();
		}
		return pass;
	}
	public void Del(final int pid)
	{
		final DBConnect db = new DBConnect();
		final String sql = "delete from PASSRECORD where PID = ?";
		db.setAutoCommit(false);
		db.prepareStatement(sql);
		try {
			db.setInt(1, pid);
			db.excute();
			db.commit();
		} catch (final Exception ex) {
			ex.printStackTrace();
		} finally {
			db.setAutoCommit(true);
			db.close();
		}
	}
}
